package com.itheima.controller;

import com.itheima.anno.LogAnnotation;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    //注入
    @Autowired
    private DeptService deptService;

    //查询所有部门的操作
    @GetMapping
    public Result queryAllDepts() {
        List<Dept> depts = deptService.queryAllDepts();
        return Result.success(depts);
    }

    //新增部门，这里可以直接传入Dept实体类，但是注解要变成@RequestBody
    //要是url请求参数上就要用@RequestParam，users?name=张三&age=20&page=2
    //如果是动态请求参数就要一同@PathVariable，/users/{id}
    @LogAnnotation
    @PostMapping
    public Result insertDept(@RequestBody Dept dept) {
        deptService.insertDept(dept);
        return Result.success();
    }

    //修改部门——1.根据id查询
    @GetMapping("/{id}")
    public Result queryByIdDept(@PathVariable Integer id) {
        Dept dept = deptService.queryByIdDept(id);
        log.info("根据id={}查询到部门信息为{}",id,dept);
        return Result.success(dept);
    }

    //修改部门——2.修改部门信息
    @PutMapping
    public Result updateDept(@RequestBody Dept dept) {
        deptService.updateDept(dept);
        log.info("要修改的部门信息是：{}",dept);
        return Result.success();
    }

    //删除部门
    @DeleteMapping
    public Result deleteDept(@RequestParam Integer id) {
        deptService.deleteDept(id);
        return Result.success();
    }

}
